<?php
namespace app\admin\model;

use think\Model;

class User extends Model{

    /**
     * 性别
     * @param id int
     * @author 巫玉梅 <1120504614@qq.com>
     * @return mixed
     */
     public function getSexAttr($value){
         $sex = [
             0 => '男',
             1 => '女'
         ];
         return $sex[$value];
     }

    /**
     * 用户身份权限
     * @param id int
     * @author 巫玉梅 <1120504614@qq.com>
     * @return mixed
     */
    public function getUserAuthAttr($value){
        $sex = [
            1 => '超级管理员',
            2 => '教师',
            3 => '学生'
        ];
        return $sex[$value];
    }


    /**
     * 查询所有用户信息
     * @author 巫玉梅 <1120504614@qq.com>
     * @return
     */
    public function getAll(){

        $order="";
        $keywords = trim(input('get.keywords'));
        if(!empty($keywords)){
            $where = ['username|full_name|tel' => ['like', "%{$keywords}%"]];
        }else{
            $where = [];
        }
        $data = $this
            ->alias('u')
            ->join('auth_group group','user_auth=group.id','LEFT')
            ->field('u.id,u.username,u.full_name,u.user_images,
                        u.tel,u.sex,group.title,u.ctime,
                        u.stop_status,group.title')
            ->where($where)
            ->order($order)
            ->select();
        if($data){
            foreach($data as $k => $v){
                $data[$k] = $v->toArray();
            }
        }

        return $data;
    }

    /**
     * 查询所有用户记录数
     * @author 巫玉梅 <1120504614@qq.com>
     * @return
     */
    public function getCount(){
        $order="";
        $keywords = trim(input('get.keywords'));
        if(!empty($keywords)){
            $where = ['username|full_name|tel' => ['like', "%{$keywords}%"]];
        }else{
            $where = [];
        }
        $count  =  $this
            ->alias('u')
            ->join('auth_group group','user_auth=group.id','LEFT')
            ->field('u.id,u.username,u.full_name,u.user_images,
                        u.tel,u.sex,group.title,u.ctime,
                        u.stop_status,group.title')
            ->where($where)
            ->order($order)
            ->count();
        return $count;
    }

    /**
     * 查询用户单条记录信息
     * @author 巫玉梅 <1120504614@qq.com>
     * @return
     */
    public function getOne($where=[],$order=""){
        $info = $this
            ->alias('u')
            ->join('auth_group group','user_auth=group.id','LEFT')
            ->field('u.*,group.title,group.title')
            ->where($where)
            ->order($order)
            ->find();
        if($info){
            $info = $info->toArray();
        }
        return $info;
    }

    /**
     * 会员表详情
     * @param id int
     * @author 巫玉梅 <1120504614@qq.com>
     * @return array|false|\PDOStatement|string|Model
     */
    public function getUserById($id){
        $where = "u.id=".$id;
        $info = $this
            ->alias('u')
            ->join('auth_group group','user_auth=group.id','LEFT')
            ->join('user rec_id','u.rec_id = rec_id.id','LEFT')
            ->join('user total_rec_id','u.total_rec_id = total_rec_id.id','LEFT')
            ->join('user province_rec_id','u.province_rec_id = province_rec_id.id','LEFT')
            ->join('user city_rec_id','u.city_rec_id = city_rec_id.id','LEFT')
            ->join('user area_rec_id','u.area_rec_id = area_rec_id.id','LEFT')
            ->join('user school_rec_id','u.school_rec_id = school_rec_id.id','LEFT')
            ->field('u.*,group.title,group.title,rec_id.full_name as rec_id
                    ,total_rec_id.full_name as total_rec_id
                    ,province_rec_id.full_name as province_rec_id
                    ,city_rec_id.full_name as city_rec_id
                    ,area_rec_id.full_name as area_rec_id
                    ,school_rec_id.full_name as school_rec_id
                    ')
            ->where($where)
            ->find();

        if($info){
           $info = $info->toArray();
        }
        return $info;
    }
}